/**   
* @Title: TestXSQZJ.java 
* @Package com.tianxiabuyi 
* @Description: TODO(用一句话描述该文件做什么) 
* @author jacking  
* @date 2018年5月29日 下午3:20:11 
* @version V1.0   
*/
package com.tianxiabuyi;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tianxiabuyi.util.GsonUtil;
import com.tianxiabuyi.util.Tools;

/** 
* @ClassName: TestXSQZJ 
* @Description: TODO(这里用一句话描述这个类的作用) 
* @author jacking
* @date 2018年5月29日 下午3:20:11 
*  
*/

public class TestXSQZJ {
	static final Integer FAILURE = 30000;
	// 获取数据库连接 add by liuzq on 20180522
public Connection getConn() { 
	String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; 
	String userName = "user"; 
	String passwrod = "123456"; 
	String url = "jdbc:sqlserver://192.168.2.72:1433;databaseName=THIS4"; 
	Connection conn = null; 
	try { 
		Class.forName(driver); 
		conn = DriverManager.getConnection(url, userName, passwrod); 
	} catch (Exception e) { 
		e.printStackTrace(); 
	} 
	return conn; 
}
	public List getPlainAppoint(Map<String, String> param) {
		Connection connection = null;
		CallableStatement statement = null;
		List<Map<String, String>> retList = new ArrayList<Map<String, String>>();
		try {
			connection = getConn();
			statement = connection.prepareCall("{? = call 获取科室预约信息(?,?,?)}"); 
			statement.registerOutParameter(1, Types.INTEGER);
			statement.setString(2, param.get("ksdm"));
			statement.setString(3, Tools.getDesc(param.get("kssj"), ""));
			statement.setString(4, Tools.getDesc(param.get("jssj"), ""));
			ResultSet rs = statement.executeQuery();
			Map<String, String> map = null;
			while(rs.next()){
				map = new HashMap<String, String>();
				map.put("ShowLable", rs.getString("ShowLable"));
				map.put("allhx", rs.getString("allhx"));
				map.put("syhx", rs.getString("syhx"));
				map.put("sjd", rs.getString("sjd"));
				map.put("zxrq", rs.getString("zxrq"));
				map.put("pbmxid", rs.getString("pbmxid"));
				retList.add(map);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
					if (statement != null)
						statement.close(); 
					// if (connection != null)
						// connection.close(); 
				} catch (Exception e) {
					e.printStackTrace();
				}
			return retList;
		}
	}
	
	/**
 * 获取排班医生信息 对应的存储过程名称 -》获取排班医生信息
 * @param ksdm 科室代码
 * @param kssj 开始时间
 * @param jssj 结束时间
 */
	public List getDoctorOnDuty(Map<String, String> param) {
		Connection connection = null;
		CallableStatement statement = null;
		List<Map<String, String>> retList = new ArrayList<Map<String, String>>();
		try {
			connection = getConn();
			statement = connection.prepareCall("{? = call 获取排班医生信息(?,?,?)}"); 
			statement.registerOutParameter(1, Types.INTEGER);

			statement.setString(2, Tools.getDesc(param.get("kssj"), ""));
			statement.setString(3, Tools.getDesc(param.get("jssj"), ""));
			statement.setString(4, param.get("ksdm"));
			ResultSet rs = statement.executeQuery();
			Map<String, String> map = null;
			while(rs.next()){
				map = new HashMap<String, String>();
				map.put("namezc", rs.getString("namezc"));
				map.put("pbkm", rs.getString("pbkm"));
				map.put("pbkmlx", rs.getString("pbkmlx"));
				map.put("zc_id", rs.getString("zc_id"));
				map.put("name", rs.getString("name"));
				retList.add(map);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
					if (statement != null)
						statement.close(); 
					// if (connection != null)
						// connection.close(); 
				} catch (Exception e) {
					e.printStackTrace();
				}
			return retList;
		}
	}
	@Test
	public void testJson(){
		
		Message msg = new Message();
		try {
			//exec 获取科室预约信息 '1021','20180518','20180601'
			String ksdm =  "1021";//科室代码
			String kssj = "20180518";//开始时间
			String jssj =  "20180601";//结束时间
			
			//获取科室预约信息
			Map<String, String> param  = new HashMap<String,String>(4);
			param.put("ksdm",ksdm);
			param.put("kssj",kssj);
			param.put("jssj",jssj);
			List appointList = getPlainAppoint(param);
			System.out.println(JSON.toJSON(appointList));
	/*		List dutyList = getDoctorOnDuty(param);
			Map ret = new HashMap<String,String>(2);
			if((appointList == null || appointList.size() == 0) && (dutyList == null || dutyList.size() == 0)) {
				msg.put("errcode",30041);
				msg.put("errmsg","暂无数据");
			} else {
				msg.put("errcode",0);
				ret.put("appointList",appointList);
				ret.put("dutyList",dutyList);
			}
			msg.put("data",ret);*/
	    }catch (Exception e){
		   	msg.put("errcode",FAILURE);
	    }
		
	}
	@Test
	public void testJson2(){
		List list = GsonUtil.fromJson("[]", new TypeToken<List> () {});
		 System.out.println(list.size());
		
	}
	@Test
	public void testJson3(){
		String sql="SELECT  top 10  t1.sfzh,t1.sfzly,t1.lrrq '录入日期',t2.* from SF_BRXXK t1,GH_SH_GHYYK t2 WHERE t1.patid=t2.patid and t1.sfzh='31010319780703202X'";
		
	}

}
